/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DBSupport;

import java.util.ArrayList;

/**
 *
 * @author Manuel
 */
public class GCMatcher implements Matcher{

    //given the list of collaborators of the form id-user_id-alternative_id and the committer id, the function returns the id of the matching collaborator
    public String matchCommitter(ArrayList<String> collaborators, String committer) {
        if(committer == null || committer.toLowerCase().equals("null") || collaborators.isEmpty())
            return "-1";
        String id = "-1";
        int match = 0;
        for(int i = 0;i<collaborators.size();i++){
            String currentEntry = collaborators.get(i);
            String[] nicknames = currentEntry.split("-");
            if(nicknames.length == 3){
                String currentCollab = nicknames[2];

                //if the collaborator matches 100% return that id
                if(committer.toLowerCase().equals(currentCollab.toLowerCase())){
                    id = nicknames[0];
                    break;
                }

                //else if a string matching must be done, assign an index of matching "match" to the solution and return highest rank
                else if(currentCollab.contains("...")){
                    String partialColl = currentCollab.substring(0,currentCollab.indexOf("...")).toLowerCase();
                    if(committer.toLowerCase().startsWith(partialColl)){
                        int currentMatch = partialColl.length();
                        if(currentMatch > match){
                            match = currentMatch;
                            id = nicknames[0];
                        }
                    }

                }
            }
        }
        return id;

    }

    //given the list of collaborators of the form id-user_id-alternative_id and the committer id, the function returns the id of the matching collaborator
    public String matchBugOwner(ArrayList<String> collaborators, String owner) {
        if(owner == null || owner.toLowerCase().equals("null") || collaborators.isEmpty())
            return "-1";

        for(int i = 0;i<collaborators.size();i++){
            String currentEntry = collaborators.get(i);
            String[] nicknames = currentEntry.split("-");
            if(nicknames.length == 3){

                //if the collaborator matches 100% return that id
                if(owner.toLowerCase().equals(nicknames[1].toLowerCase())){
                    return(nicknames[0]);
                }
            }
        }
        return "-1";
    }

}
